package com.mh.shiro.business.service.impl;

import com.mh.shiro.business.entity.UserRole;
import com.mh.shiro.business.service.SysUserRoleService;
import com.mh.shiro.persistence.beans.SysUserRole;
import com.mh.shiro.persistence.mapper.SysUserRoleMapper;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年06月23日15:29
 * @Description: 用户角色
 */
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleMapper resourceMapper;

    /**
     * 根据用户ID删除用户角色
     *
     * @param userId
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
    public void removeByUserId(Long userId) {
        Example example = new Example(SysUserRole.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("userId", userId);

        resourceMapper.deleteByExample(example);
    }

    /**
     * 添加用户角色
     *
     * @param userId
     * @param roleIds
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
    public void addUserRole(Long userId, String roleIds) {
        //删除
        removeByUserId(userId);
        //添加
        String[] roleIdArr = roleIds.split(",");
        if (roleIdArr.length == 0) {
            return;
        }
        UserRole u = null;
        List<UserRole> roles = new ArrayList<>();
        for (String roleId : roleIdArr) {
            if (StringUtils.isEmpty(roleId)) {
                continue;
            }
            u = new UserRole();
            u.setUserId(userId);
            u.setRoleId(Long.parseLong(roleId));

            roles.add(u);
        }

        insertList(roles);
    }

    @Override
    public UserRole insert(UserRole entity) {
        return null;
    }

    /**
     * 批量插入，支持批量插入的数据库可以使用，另外该实体限制包含id属性并且必须为自增列
     * @param entities
     */
    @Override
    public void insertList(List<UserRole> entities) {
        Assert.notNull(entities,"entities不可为空");
        if(StringUtils.isEmpty(entities)) {
            return ;
        }
        List<SysUserRole> sysUserRole = new ArrayList<>();
        for(UserRole ur : entities) {
            ur.setCreateTime(new Date());
            ur.setUpdateTime(new Date());

            sysUserRole.add(ur.getSysUserRole());
        }

        resourceMapper.insertList(sysUserRole);
    }

    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    public boolean removeByPrimaryKey(Long primaryKey) {
        return resourceMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    public boolean update(UserRole entity) {
        return false;
    }

    @Override
    public boolean updateSelective(UserRole entity) {
        return false;
    }

    @Override
    public UserRole getByPrimaryKey(Long primaryKey) {
        return null;
    }

    @Override
    public UserRole getOneByEntity(UserRole entity) {
        return null;
    }

    @Override
    public List<UserRole> listAll() {
        return null;
    }

    @Override
    public List<UserRole> listByEntity(UserRole entity) {
        return null;
    }
}
